package com.game.manager
{
    import com.dusk.game.Logger;
    import com.dusk.util.BitmapUtil;
    import com.dusk.util.FilterUtil;
    import com.dusk.util.ResUtil;
    import com.dusk.util.TextUtil;
    import com.dusk.util.UtilBase;
    
    import flash.display.Bitmap;
    
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    
    import flash.text.TextField;
    
    import flash.text.TextFormatAlign;
    import flash.utils.Dictionary;
    
    public class GameGadgetManager
    {
        private static var numberCache:Dictionary = new Dictionary();
        
        /**
         * 使用默认的字体获取数值图片
         * @param value 数字值，可以是String或者int
         * @param colors 渐变数组，由上到下进行颜色渐变，可以传入多个颜色的渐变，但是API限制不打算开放自定义渐变区域
         * @param fontSize 字体大小，默认30
         * @param glowColor 描边颜色，默认黑色
         * @return
         */
        public static function getFilterGadget(value:*, colors:Array = null, fontSize:Number = 30, glowColor:uint = 0):DisplayObject
        {
            //版本2.0通过对TextField对象应用渐变滤镜来达到文字颜色渐变的效果，既可以生成位图，也可以生成文字矢量图
            /**
             * 对目标添加滤镜
             */
            function wrapFilter(a:DisplayObject):void
            {
                FilterUtil.setGradientBevel(a,10,-90,colors,alphas,ratios,10,10,4,3,"inner",false);
                FilterUtil.setGlow(a,0xFFFFFF,1,5,5,4);
                FilterUtil.setGlow(a,glowColor,1,4,4,5);
                FilterUtil.clearCache();
            }
            var type:int = SettingManager.getSettingVO().indicateAniType;
            if(type == AnimationManager.ANIMATE_TYPE_BITMAP)
            {
                var key:String = transformKey(value,colors,fontSize);
                if(numberCache[key])
                {
                    return new Bitmap(numberCache[key]);
                }
            }
            //确定没有缓存，开始生成
            if(colors.length > 1)
            {
                //如果有多个颜色，则使用纵向渐变
                var alphas:Array = [];
                var ratios:Array = [];
                for(var i:int = 0;i < colors.length;i++)
                {
                    alphas.push(1);
                    ratios.push(i * 255 / (colors.length - 1));
                }
            }
            var text:TextField = TextUtil.creatTextField(value.toString(),TextUtil.UNIVERSAL_FONT,fontSize,TextFormatAlign.LEFT,colors[0],true);
            var sp:Sprite = new Sprite();
            sp.addChild(text);
            wrapFilter(text);
            if (type == AnimationManager.ANIMATE_TYPE_MOVIE_CLIP)
            {
                return sp;
            }
            else
            {
                var bmd:BitmapData = BitmapUtil.drawSpriteToBitmap(sp);
                numberCache[key] = bmd;
                return new Bitmap(bmd);
            }
            //版本1.0，通过对于bitmap的渐变色绘画实现文字变色，缺点是先变色再应用滤镜，只能生成位图
            /*/!**
             * 对于新建的对象描边处理
             *!/
            function glow(a:*):*
            {
                FilterUtil.setGlow(a,0xFFFFFF,1,5,5,4);
                FilterUtil.setGlow(a,glowColor,1,4,4,5);
                FilterUtil.clearCache();
                return a;
            }
            if(colors == null)
            {
                colors = [0xFFFF00,0xFF0000];
            }
            //由于glowColor是最后一步处理的，没必要存储相同的颜色
            var key:String = transformKey(value,colors,fontSize);
            if(numberCache[key])
            {
                return glow(new Bitmap(numberCache[key]));
            }
            var txt:TextField = TextUtil.creatTextField(Math.abs(value).toString(),TextUtil.UNIVERSAL_FONT,fontSize,TextFormatAlign.LEFT,colors[0]);
            var sp:Sprite = new Sprite();
            sp.addChild(txt);
            var bmd:BitmapData = BitmapUtil.drawSpriteToBitmap(sp);
            if(colors.length > 1)
            {
                //如果有多个颜色，则使用纵向渐变
                var alphas:Array = [];
                var ratios:Array = [];
                for(var i:int = 0;i < colors.length;i++)
                {
                    alphas.push(1);
                    ratios.push(i * 255 / (colors.length - 1));
                }
                bmd = BitmapUtil.applyGradientToBitmapData(bmd,colors,alphas,ratios);
            }
            numberCache[key] = bmd;
            return glow(new Bitmap(numberCache[key]));*/
        }
        
        public static function getStyledNumberBitmap(resPrefix:String,value:*):Bitmap
        {
            var key:String = transformKey(resPrefix,value);
            if(numberCache[key])
            {
                return new Bitmap(numberCache[key]);
            }
            var bm:Bitmap = ResUtil.getImageBitmap(resPrefix + value);
            numberCache[key] = bm.bitmapData;
            return bm;
        }
        
        public static function getImage(resName:String):Bitmap
        {
            var key:String = transformKey(resName);
            if(numberCache[key])
            {
                return new Bitmap(numberCache[key]);
            }
            var bm:Bitmap = ResUtil.getImageBitmap(resName);
            numberCache[key] = bm.bitmapData;
            return bm;
        }
        
        private static function transformKey(...args):String
        {
            var key:String = "";
            for each (var arg:* in args)
            {
                key += "|" + arg.toString() + "|";
            }
            return key;
        }
        
        public static function clearAllCache():void
        {
            UtilBase.getAllKeys(numberCache).forEach(function(key:String,...a):void
            {
                (numberCache[key] as BitmapData).dispose();
                Logger.log("delete key: " + key)
                delete numberCache[key];
            });
            Logger.log("====== clear number cache ======");
        }
    }
}
